<?php

namespace app\backstage\validate;

use think\Validate;

class NovelValidate extends Validate
{
    protected $rule = [
        'id' => 'require|gt:0',
        'title' => 'require|max:100',
        'author' => 'require|max:30',
        'cover' => 'require',
        'in_shelf' => 'require|in:0,1,2',
        'update_status' => 'require|in:0,1,2',
        'scale' => 'require|in:S,A,B,C',
        'freq_id' => 'require|gt:0',
        'class_id' => 'require|gt:0',
        'label' => 'array',
        'brief' => 'require',
        'type' => 'require|in:1,2',
        'novel_id' => 'require|gt:0',
        'chapter_no' => 'require|gt:0',
        'name' => 'require|max:100',
        'is_pay' => 'require|in:0,1',
        'content' => 'require',
        'lead' => 'max:30',
        'ex_nid' => 'require|gt:0',
        'agents' => 'checkIds',
        'is_add' => 'require|in:0,1',
        'recom_chapter' => 'number|gt:0|elt:50',
        'words_fee' => 'require|number|egt:0',
        'freqIds' => 'require|array',
        'wgt' => 'require|number|egt',
        'nidArr' => 'require|array',
    ];

    protected $message = [
        'cover.require' => '请上传封面',
        'ex_nid.require' => '请选择书籍',
        'recom_chapter.number' => '请推荐收费章节输入正确的值',
        'recom_chapter.gt' => '推荐收费章节必须>0',
        'recom_chapter.elt' => '推荐收费章节必须<=50',
        'words_fee.number' => '请千字/书币输入正确的值',
        'words_fee.gt' => '千字/书币必须>=0',
        'freqIds.require' => '必须选择频道',
        'freqIds.array' => '频道数据格式异常',
        'wgt' => '权重是大于等于0的整数',
    ];

    protected $scene = [
        'edit' => ['id', 'title', 'author', 'lead', 'cover', 'in_shelf', 'update_status', 'scale', 'freq_id', 'class_id', 'brief', 'recom_chapter', 'words_fee'],
        'upload' => ['id', 'type'],
        'add' => ['title', 'author', 'lead', 'cover', 'in_shelf', 'update_status', 'scale', 'freq_id', 'class_id', 'brief', 'words_fee'],
        'preview' => ['novel_id', 'chapter_no'],
        'setExclusive' => ['ex_nid', 'agents', 'is_add'],
        'setClassWgt' => ['id', 'wgt'],
        'renderNovels' => ['nidArr'],
    ];


    public function sceneChapterForm()
    {
        return $this->only(['novel_id', 'chapter_no'])
            ->remove('chapter_no', 'require');
    }


    public function sceneSetChapter()
    {
        return $this->only(['novel_id', 'chapter_no', 'name', 'is_pay', 'content'])
            ->remove('chapter_no', 'require');
    }


    public function sceneSetLabel()
    {
        return $this->only(['id', 'name'])
            ->remove('id', 'require');
    }

    // 自定义验证规则
    protected function checkIds($value, $rule, $data = [])
    {
        if (!preg_match('#^[1-9][0-9]*(,[1-9][0-9]*)*$#', $value)) {
            return '参数异常';
        }
        return true;
    }

    //验证添加频道
    public function sceneSetFreq()
    {
        return $this->only(['id','name'])
            ->remove('id', 'require');
    }

    //删除频道
    public function sceneDelFreq()
    {
        return $this->only(['id']);
    }

    //删除分类
    public function sceneDelClass()
    {
        return $this->only(['id']);
    }

    public function sceneDelClassSon()
    {
        return $this->only(['id']);
    }

    public function sceneSetClass()
    {
        return $this->only(['id', 'name', 'freqIds'])
            ->remove('id', 'require');
    }

    public function sceneSetClassSon()
    {
        return $this->only(['id', 'name', 'freq_id', 'class_id'])
            ->remove('id', 'require');
    }
}